﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using com.huawei.esdk.tp.professional.eSDKTPCommon;
using com.huawei.esdk.tp.professional.Utils;


namespace com.huawei.esdk.tp.professional.local
{
    public partial class AuthorizeServiceEx
    {
        private static string url = com.huawei.esdk.tp.professional.Utils.UtilsSettings.URL + com.huawei.esdk.tp.professional.Utils.UtilsSettings.tpCommon;
        private static eSDKTPCommon.TPCommonService tpCommon = new eSDKTPCommon.TPCommonService(url);
        public static System.Net.CookieContainer cookieJar = new System.Net.CookieContainer();

        RSA2048 rsa2048 = new RSA2048();
        public static string publicKey;
        public static byte[] secretKey;
        public static byte[] iv;

        private static AuthorizeServiceEx instance = null;
        protected AuthorizeServiceEx()
        {

        }
        public static AuthorizeServiceEx Instance()
        {
            if (null == instance)
            {
                instance = new AuthorizeServiceEx();
            }
            return instance;
        }

        public int Login(string userName, string password)
        {
            try
            {
                if (null == cookieJar)
                {
                    cookieJar = new System.Net.CookieContainer();
                }
                tpCommon.CookieContainer = cookieJar;
                byte[] randomSequence;
                string hashType = "";
                int reponse = tpCommon.LoginRequest(userName, "API", 2, true, out randomSequence, out hashType);

                if (reponse == 0)
                {
                    string encodePwd = Base64Utils.encode(randomSequence);
                    string pwd = Encrypt.getEncryptPassword(null == encodePwd ? "" : encodePwd, password);
                    byte[] base64Pwd = Base64Utils.getFromBASE64(null == pwd ? "" : pwd);
                    int result = tpCommon.Authenticate(null == base64Pwd ? new byte[0] : base64Pwd);

                    if (result == 0)
                    {
                        publicKey = rsa2048.getPublicKey();
                        secretKey = rsa2048.GetRandomNumber();
                        iv = rsa2048.GetRandomNumber();
                        result = rsa2048.SendSecretKey(secretKey, iv, publicKey);
                    }
                    
                    return result;
                }
                return reponse;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }
        }

        public int Logout()
        {
            try
            {
                tpCommon.CookieContainer = AuthorizeServiceEx.cookieJar;
                int result = tpCommon.Logout();
                if (0 == result)
                {
                    AuthorizeServiceEx.cookieJar = null;
                }
                return result;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }



        }

        public int KeepAlive()
        {
            try
            {
                tpCommon.CookieContainer = AuthorizeServiceEx.cookieJar;
                int result = tpCommon.KeepAlive();
                return result;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }

        }
    }
}
